//
//  RoomsHandler.m
//  mUBB
//
//  Created by Cosmin Stirbu on 1/18/13.
//  Copyright (c) 2013 Cosmin Stirbu. All rights reserved.
//

#import "RoomsHandler.h"
#import "RoomsProtocol.h"
#import "Room.h"
#import "Building.h"
#import "ListWrapper.h"


@implementation RoomsHandler

- (void)handleSuccess:(WSResponse *)response {
    ListWrapper *roomsList = [ListWrapper new];
    NSDictionary *roomsDictionary = [response.data objectFromJSONData];
    NSArray *jsonRooms = [roomsDictionary getJSONObjectForKey:ITEMS_TAG];
    for (NSDictionary *roomDictionary in jsonRooms) {
        Room *room = [Room new];
        room.building = [Building new];
        
        // Room Id
        room.roomId = [[roomDictionary getJSONObjectForKey:ROOM_ID_TAG] integerValue];
        // Room Name
        room.name = [roomDictionary getJSONObjectForKey:ROOM_NAME_TAG];
        // Self URL
        room.selfUrl = [roomDictionary getJSONObjectForKey:SELF_URL_TAG];
        // Building name
        room.building.name = [roomDictionary getJSONObjectForKey:BUILDING_NAME_TAG];
        [roomsList.itemsList addObject:room];
    }
    roomsList.totalNumberOfItems = [[roomsDictionary getJSONObjectForKey:TOTAL_NUMBER_OF_ITEMS_TAG] integerValue];
    [self.delegate didReceiveRoomsWithSuccess:roomsList forTicket:self.ticket];
}

- (void)handleError:(WSError *)error {
    [self.delegate didReceiveRoomsWithFailure:error forTicket:self.ticket];
}

@end
